package com.epoint.mobileoa.ui.filechoose;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.ui.init.InitView;

/**
 * @author liyc
 * @time 2012-4-10 下午4:47:27
 * @annotation 
 */
public class FileChooseView extends SuperView {

	public static final String FILE_TYPE_BACK = "back";
	public static final String FILE_TYPE_DIR = "dir";
	public static final String FILE_TYPE_FILE = "file";
	public static final String PHONE_ROOT_PATH = "/system";
	public static final String FILE_ROOT_PAHT ="";
	public static Map<String, String> fileMap = new HashMap<String, String>();

	private ListView lv;
	private List<Map<String, Object>> mData;
	private ArrayList<String> backflaglist;// 用作返回按钮作标记

	@Override
	public void initControls() {
		top_left_bt.setOnClickListener(this);
		backflaglist = new ArrayList<String>();
		
		String initpath = getIntent().getStringExtra("initPath")==null?"":getIntent().getStringExtra("initPath");
		
		mData = getData(initpath);  
		
		lv = (ListView) findViewById(R.id.filechooselv);
		MyAdapter adapter = new MyAdapter(this);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try{
					if(mData.get(position).get("type").equals(FILE_TYPE_DIR)){
						String path = mData.get(position).get("path").toString();
						backflaglist.add(path);
						mData = getData(path);  
						MyAdapter adapter = new MyAdapter(FileChooseView.this);
						lv.setAdapter(adapter);
					}
					else if(mData.get(position).get("type").equals(FILE_TYPE_BACK)){
						backLogic();
					}
					else{
						File file = new File(mData.get(position).get("path").toString());
						if(file.length()/1024>2048){
							ToastHelp.showToast(FileChooseView.this, "请选择上传文件小于2M的附件");
							return;
						}
						fileMap.put("name",mData.get(position).get("name").toString());
						fileMap.put("path",mData.get(position).get("path").toString());
						finish();
					}
				}catch(Exception e){
					e.printStackTrace();
					fileMap.clear();
					finish();
				}
			}
		});

	}

	private List<Map<String, Object>> getData(String dir) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  

		Map<String, Object> map;  

		File file=new File(dir);
		File[]  files = file.listFiles();

		if(dir.equals("")){
			map = new HashMap<String, Object>();  
			map.put("type", FILE_TYPE_DIR);
			map.put("path", "/"+InitView.MobileOaMainStoreFolder);
			map.put("name", "SD卡");
			map.put("icon", R.drawable.fc_sdcard);  
			list.add(map);  

			map = new HashMap<String, Object>();  
			map.put("type", FILE_TYPE_DIR);
			map.put("path", PHONE_ROOT_PATH);
			map.put("name", "系统文件");
			map.put("icon", R.drawable.fc_system);  
			list.add(map);  
		}
		else{
			map = new HashMap<String, Object>();  
			map.put("type", FILE_TYPE_BACK);
			map.put("path", "");
			map.put("name", "...");
			map.put("icon", R.drawable.fc_back);  
			list.add(map);  

			for(File currentFile : files){
				if(currentFile.isDirectory()){
					map = new HashMap<String, Object>();  
					map.put("type", FILE_TYPE_DIR);
					map.put("path", currentFile.getAbsolutePath());
					map.put("name", currentFile.getName());
					map.put("icon", R.drawable.fc_dir);  
					list.add(map);  
				}
			}
			for(File currentFile : files){
				if(!currentFile.isDirectory()){
					map = new HashMap<String, Object>();  
					map.put("type", FILE_TYPE_FILE);
					map.put("path", currentFile.getAbsolutePath());
					map.put("name", currentFile.getName());
					map.put("icon", R.drawable.fc_file);  
					list.add(map);  
				}
			}
		}
		return list;  
	} 

	public void backLogic(){
		/**
		 * 返回按钮操作逻辑 liyc 2011.08.02
		 */
		if (backflaglist.size() == 0) {
			fileMap.clear();
			finish();
		} else if (backflaglist.size() == 1) {
			mData = getData(FILE_ROOT_PAHT);
			MyAdapter adapter = new MyAdapter(
					FileChooseView.this);
			lv.setAdapter(adapter);
		} else {
			mData = getData(backflaglist.get(backflaglist
					.size() - 2));
			MyAdapter adapter = new MyAdapter(
					FileChooseView.this);
			lv.setAdapter(adapter);
		}
		if (backflaglist.size() > 0) {
			backflaglist.remove(backflaglist.size() - 1);
		}
	}

	@Override
	public void refresh(Object... param) {
	}

	@Override
	public int getLayoutInt() {
		return R.layout.filechooseview;
	}

	public final class ViewHolder{  
		public ImageView fc_img;  
		public TextView fc_title;  
	} 

	public class MyAdapter extends BaseAdapter{  
		private LayoutInflater mInflater;  
		public MyAdapter(Context context){
			this.mInflater = LayoutInflater.from(context);  
		}

		@Override 
		public int getCount() {
			return mData.size();  
		}

		@Override 
		public Object getItem(int arg0) {
			return null;  
		}



		@Override 
		public long getItemId(int arg0) {
			return 0;  
		}

		@Override 
		public View getView(int position, View convertView, ViewGroup parent){
			ViewHolder holder = null;
			if (convertView == null) {
				holder=new ViewHolder();
				convertView = mInflater.inflate(R.layout.listviewfile, null);
				holder.fc_img = (ImageView)convertView.findViewById(R.id.fc_img);
				holder.fc_title = (TextView)convertView.findViewById(R.id.fc_title);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder)convertView.getTag();
			}
			holder.fc_img.setBackgroundResource((Integer)mData.get(position).get("icon"));
			holder.fc_title.setText((String)mData.get(position).get("name"));
			setTextViewFontColor(holder.fc_title);
			return convertView;  
		}  
	} 

	@Override
	public String setTitle() {
		return "文件选择";
	}

	@Override
	public void KEYCODE_BACK() {
		backLogic();
	}

	@Override
	public void onClick(View v) {
		if(v==top_left_bt){
			fileMap.clear();
			finish();
		}
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
